package com.ly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.mapper.EdocCategoryMapper;
import com.ly.mapper.EdocEntryMapper;
import com.ly.pojo.EdocCategory;
import com.ly.pojo.EdocEntry;
import com.ly.service.EdocEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EdocEntryServiceImpl extends ServiceImpl<EdocEntryMapper, EdocEntry> implements EdocEntryService {
    @Autowired
    private EdocCategoryMapper categoryMapper;
    @Autowired
    private EdocEntryMapper edocEntryMapper;

    @Override
    public Long count(Integer categoryId) {
        QueryWrapper<EdocEntry> queryWrapper = new QueryWrapper<>();
        if(categoryId != null){
            queryWrapper.eq("categoryId",categoryId);
        }
        return edocEntryMapper.selectCount(queryWrapper);
    }


    @Override
    public List<EdocEntry> list(Integer categoryId, Integer startIndex, Integer pageSize) {
        Integer indexOf = (startIndex - 1) * pageSize;
        return edocEntryMapper.list(categoryId, indexOf, pageSize);
    }

    @Override
    public List<EdocCategory> categoryList() {
        return categoryMapper.selectList(null);
    }

    @Override
    public Integer add(EdocEntry edocEntry) {
        if(edocEntry.getCategoryId() == 0L) {
            edocEntry.setCategoryId(1L);
        }
        return edocEntryMapper.insert(edocEntry);
    }

    @Override
    public EdocEntry getById(Integer id) {
        return edocEntryMapper.selectById(id);
    }

    @Override
    public Integer delete(Integer id) {
        return edocEntryMapper.deleteById(id);
    }

    @Override
    public Integer update(EdocEntry edocEntry) {
        return edocEntryMapper.updateById(edocEntry);
    }
}
